<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" errorPage="/error/error.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctxPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加楼层</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctxPath}/layui/css/layui.css" media="all">
</head>
<body>

<fieldset class="layui-elem-field layui-field-title"
          style="margin-top: 20px;">
    <legend>新增楼层页</legend>
</fieldset>

<script src="${ctxPath}/js/jquery-1.11.1.min.js"></script>

<form id="roomForm" class="layui-form"
      action=""
      method="post">

    <div class="layui-form-item">
        <label class="layui-form-label">楼层</label>
        <div class="layui-input-block" style="width:450px;">
            <input type="text" id="floor"
                   name="floor" autocomplete="off"
                   placeholder="请输入楼层"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block" style="width:450px;">
            <input type="text" id="descript"
                   name="descript" autocomplete="off"
                   placeholder="请输入描述"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn"
                    onclick="goSubmit();">
                创建楼层
            </button>
            <button type="button" class="layui-btn layui-btn-primary">
                重置表单
            </button>
        </div>
    </div>

</form>
<script>


    // 提交的参数列表
    var items = [
        "floor", "descript"
    ];

    //{ps} 抓取表单数据
    function pickData() {
        //定义一个对象, 存放所有数据。
        var obj = {};
        for (var i = 0; i < items.length; i++) {
            var id = items[i];  //获取表单项 id
            var val = $("#" + id).val();
            console.log(id+":"+val);
            //{3}填入 obj 对象
            obj[id] = val;
        }
        return obj;
    }

    function goSubmit() {
        var obj = pickData();  //抓取表单数据..
        console.log("pickData:" + obj);
        //{ps}提交  /User/saveUser
        $.ajax(
            {
                url: '/storey/saveStorey',
                type: 'post',
                data: obj,
                dataType: 'json',
                success: function (respTxt) {
                    console.log("respTxt:" + respTxt);
                    var ret = respTxt['result'];
                    if (ret == 'success') {
                        layer.msg('添加成功:' + obj.floor);
                        setTimeout(
                            function () {
                                //{ps}前端浏览器跳转到[前端重定向]
                                window.location = '${ctxPath}/storey/viewList';
                            }, 1000);
                    } else {
                        layer.msg(respTxt.cause);
                    }
                }
            }
        );
    }
</script>


<script src="${ctxPath}/layui/layui.js" charset="utf-8">
</script>
<script>


    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        // form.verify({
        //     account: function (value) {
        //         if (value.length < 3) {
        //             return '帐号至少 3 个字符';
        //         }
        //     }
        //     , pass: [
        //         /^[\S]{3,3}$/
        //         , '密码必须 3 到 3 位，且不能出现空格'
        //     ]
        //     , content: function (value) {
        //         layedit.sync(editIndex);
        //     }
        // });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        // 监听提交
        form.on('submit(demo1)', function (data) {
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })
            return false;
        });

        // 表单初始赋值
        // form.val('example', {
        //     "username": "贤心" // "name": "value"
        //     , "password": "123456"
        //     , "interest": 1
        //     , "like[write]": true //复选框选中状态
        //     , "close": true //开关状态
        //     , "sex": "女"
        //     , "desc": "我爱 layui"
        // })


    });
</script>
